#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 1994, 1995, 1996 by Sun Microsystems, Inc.
# All  Rights Reserved
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
#	This script will automatically generate a "printcap" file
#	from the currently configured LP printer configuration.
#
PATH=/bin:/usr/bin:/usr/sbin

TEXTDOMAIN="SUNW_OST_OSCMD"
export TEXTDOMAIN

export PATH
umask 022

set -- `getopt d:f: $*`
if [ $? != 0 ] ; then
	echo "Usage: $0 [-d dir] [-f file]"
	exit 1
fi

for OPTION in $*
do
	case $OPTION in
	-f) SYSTEM_FILE=$2; shift 2;;
	-d) BASE_DIR=$2; shift 2;;
	--) shift; break;;
	esac
done

SYSTEM_FILE=${SYSTEM_FILE:-"${BASE_DIR}/etc/printers.conf"}

if [ ! -d ${BASE_DIR}/etc/lp/printers ] ; then
	gettext "Exit $0: There is no directory ${BASE_DIR}/etc/lp/printers\n\tfrom which to create /etc/printers.conf."
	exit 0
fi

if [ -f ${BASE_DIR}/etc/lp/default ] ; then
	DEFAULT_PRINTER=`cat ${BASE_DIR}/etc/lp/default`
	lpset -n system -a "use=${DEFAULT_PRINTER}" _default
	mv ${BASE_DIR}/etc/lp/default ${BASE_DIR}/etc/default.orig
fi

cd ${BASE_DIR}/etc/lp/printers	# get the list of locally configured printers
PRINTERS=`echo *`

for PRINTER in ${PRINTERS}	# for each printer get config info
do
    if [ "${PRINTER}" = "*" ] ; then
	continue
    fi

    RNAME=${PRINTER}
    DESC=""
    RHOST=""

    if [ -f ${PRINTER}/comment ] ; then
	    DESC=`cat ${PRINTER}/comment`
    fi

    REMOTE=`grep Remote: ${PRINTER}/configuration 2>/dev/null | sed -e "s/^Remote: //"`
    DEVICE=`grep Device: ${PRINTER}/configuration 2>/dev/null | sed -e "s/^Device: //"`

    if [ -n "${DEVICE}" ] ; then
	RHOST=`uname -n`
    elif [ `echo ${REMOTE} | grep -c \!` -ne 0 ] ; then
	RHOST=`echo $REMOTE | cut -d \! -f 1`
	RNAME=`echo $REMOTE | cut -d \! -f 2`
    else
	RHOST=${REMOTE}
    fi

    lpset -n system -a "bsdaddr=${RHOST},${RNAME}" -a "description=${DESC}" \
	${PRINTER}

done

exit 0
